# 利用tailhead软件包跟踪文件变化
import tailhead
# 字符串解析
import parse
# 时间包
import datetime
import os
import time
import regex as re

# 调用客户端
from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient

# 用于自动生成命令行
import fire

# 输出log，便于调试
import logging

def main(host='test2.bj-jiashi.com',
        port=10006,
        token='AE33', 
        delay_time=0.5, 
        target_log_file='sub3_AE33_simulator.py.test.log',
        log_level = 'INFO'):
    # 初始化
    logging.basicConfig(level = getattr(logging,log_level.upper(),logging.INFO)) # 默认info
    file_base_name = os.path.basename(__file__)
    logger = logging.getLogger(file_base_name)

    # header
    before_header="""AETHALOMETER
Serial number = AE33-S04-00358
Application version = 1.4.2.0
Number of channels = 7

"""
    # 分隔符
    sep = ' '
    col_name_sep = ';'
    # 列名
    header = 'Date(yyyy/MM/dd); Time(hh:mm:ss); Timebase; RefCh1; Sen1Ch1; Sen2Ch1; RefCh2; Sen1Ch2; Sen2Ch2; RefCh3; Sen1Ch3; Sen2Ch3; RefCh4; Sen1Ch4; Sen2Ch4; RefCh5; Sen1Ch5; Sen2Ch5; RefCh6; Sen1Ch6; Sen2Ch6; RefCh7; Sen1Ch7; Sen2Ch7; Flow1; Flow2; FlowC; Pressure(Pa); Temperature(°C); BB(%); ContTemp; SupplyTemp; Status; ContStatus; DetectStatus; LedStatus; ValveStatus; LedTemp; BC11; BC12; BC1; BC21; BC22; BC2; BC31; BC32; BC3; BC41; BC42; BC4; BC51; BC52; BC5; BC61; BC62; BC6; BC71; BC72; BC7; K1; K2; K3; K4; K5; K6; K7; TapeAdvCount;'
    display_names = [n.strip() for n in header.split(col_name_sep)[:-1]]
    col_names = header.split(col_name_sep)
    # re.sub('[^a-zA-Z0-9]','_',n.strip())
    # 把'Temperature(°C)'转化为'Temperature__C_'
    ## 注意，原始数据只有67列，但是下面的数据有70列！！
    col_names = 'yyyy,MM,dd,hh,mm,ss'.split(',')+ [re.sub('[^a-zA-Z0-9]','_',n.strip())  for n in col_names[2:-1]] + ['Unknown1','Unknown2','Unknown3']
    # 格式
    # example 
    # 2018/11/27 08:36:18 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3461 1548 0 101325 20 0.0 5 10 1 0 20 10 00000 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.007738049 0.007743395 0.008087777 0.00822126 0.009227347 0.007967672 0.009248562 1326 5 0 0
    # datetime = 2018/11/27 08:36:18
    data_write_formats = ['d']*6+['d']*27+['f']+['d']*29+['f']*7+['d']*4
    data_read_formats = [format[-1] for format in data_write_formats]

    # data types
    data_types = dict(zip(col_names, data_read_formats))

    # selected cols
    selected_cols = [col.strip() for col in 'BC6'.split(',') ]
    
    # 默认值，为保持完整性
    defaults_str = '2018/11/27 12:10:28 1 850506 803293 958959 987875 646232 721277 945804 650772 714832 819977 617467 675464 913549 766363 823828 749052 915690 978777 785257 921569 972926 2006 985 2991 101325 20 0.0 26 41 0 0 10 10 00000 26 1258 1645 1433 1284 1668 1361 356 635 374 1213 1384 1263 96 -823 100 -335 -682 -341 -81 1351 -83 0.007738049 0.007743395 0.008087777 0.00822126 0.009227347 0.007967672 0.009248562 1327 5 0 0'
    # 第一行
    logger.info(header)

    # 生成格式化字符串
    datetime_formatter = '{yyyy:4d}/{MM:2d}/{dd:2d} {hh:2d}:{mm:2d}:{ss:2d}'
    line_write_formatter=datetime_formatter +sep + sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names[6:],data_write_formats[6:])])
    logger.info(line_write_formatter)

    datetime_formatter = '{yyyy:d}/{MM:d}/{dd:d} {hh:d}:{mm:d}:{ss:d}'
    line_read_formatter=datetime_formatter +sep + sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names[6:],data_read_formats[6:])])
    logger.info(line_read_formatter)

    # 简单测试
    parse_result = parse.parse(line_read_formatter,defaults_str)
    logger.info(parse_result)
    if parse_result is None:
        raise RuntimeError('Something go wrong!')

    # 默认值
    data_dict_defaults = parse_result.named

    # example data
    # raw_data_example_filename = r"raw_data\课题3\POPS\POPS_20210107x003.csv"
    
    # 建立设备连接
    TBD = TBDeviceMqttClient(host, port, token)
    TBD.connect()
    logger.info('mqtt server connected!')

    for line in tailhead.follow_path(target_log_file,encoding='utf-8'):
        if line is not None:
            logger.debug(f"readline: {line}")
            # 解析
            parse_result = parse.parse(line_read_formatter, line)
            if parse_result is None:
                logger.debug('not a valid data line')
                continue 
            data_line_dict = parse_result.named

            data_dict = {'ts':-1,'values':{}}
            # 时间解析
            # 假设是UTC时间，如果是北京时间需要调整
            time_datetime = datetime.datetime(  year=data_line_dict['yyyy'],
                                month=data_line_dict['MM'],
                                day=data_line_dict['dd'],
                                hour=data_line_dict['hh'],
                                minute=data_line_dict['mm'],
                                second=data_line_dict['ss'])

            data_dict['ts'] = int(time_datetime.timestamp()*1000) # 转换为ms数

            # 数据抽取
            data_dict['values'] = {col:data_line_dict.get(col,-99999) for col in selected_cols}

            logger.info(f'data_dict={data_dict}')

            # 上传
            TBD.send_telemetry(data_dict)
            time.sleep(delay_time)
        else:
            logger.info('sleep.')
            time.sleep(delay_time)

if __name__ == "__main__":
    fire.Fire(main)
    # e.g
    # python demo_tail_log_upload.py --DELAY 0.5 --LOG_FILE your_file.log